<?php

	include("config.php");
    include(INCLUDEDIR."core.php");
	
	if(session_get("logged")=="user") {
		header("location: main.php");	
		exit();
	}
	
	$tpl = new Sitio();
	$tpl->setPlantilla("tpl.login.html");
	$tpl->prepare();
	$email = isset($_POST['email'])?sanitizar($_POST['email']):"";
	$password = isset($_POST['password'])?$_POST['password']:"";
	$referer = isset($_REQUEST['referer'])?urldecode($_REQUEST['referer']):"index.php";
	
	if(isset($_POST['accion']) && $_POST['accion']=='dologin') {
		$referer = isset($_POST['referer'])?$_POST['referer']:"main.php";
		if(isset($_POST['email']) && isset($_POST['password'])) {
			$email = $_POST['email'];
			$password = $_POST['password'];
			$u = new Usuario();
			if($u->validate($email,$password)) {
				$u->login($email);
				redir($referer);				
			} else {
				$tpl->setError($u->last_error);
			}
		} else {
			$tpl->setError("Debe escribir un usuario y una clave");
		}
	} 
	$tpl->assign("referer",$referer);
	$tpl->Mostrar();
	

?>